﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien1.BUS;
using QuanLyThuVien1.DAO;
using QuanLyThuVien1.DTO;

namespace QuanLyThuVien1.GUI
{
    public partial class Fr_TraSach : Form
    {
        public Fr_TraSach()
        {
            InitializeComponent();
        }
        public static int tienPhat = 0;

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public string ChuanHoa(string m_sChuoi)
        {
            if (m_sChuoi == "")
                return m_sChuoi;
            for (int i = 0; i < m_sChuoi.Length - 1; )
            {
                if (m_sChuoi[i] == 32 && m_sChuoi[i + 1] == 32)
                {
                    m_sChuoi = m_sChuoi.Substring(0, i + 1) + m_sChuoi.Substring(i + 2, m_sChuoi.Length - i - 2);
                }
                else
                    i++;
            }
            if (m_sChuoi[0] == 32 && m_sChuoi.Length == 1)
                return "";

            if (m_sChuoi[0] == 32)
                m_sChuoi = m_sChuoi.Substring(1, m_sChuoi.Length - 1);
            if (m_sChuoi[m_sChuoi.Length - 1] == 32)
                m_sChuoi = m_sChuoi.Substring(0, m_sChuoi.Length - 1);
            return m_sChuoi;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CTongQuat_DAOcs t = new CTongQuat_DAOcs();
            CPhieuTra_Bus p = new CPhieuTra_Bus();
            PHIEUTRA p1 = new PHIEUTRA();
           
            if (ChuanHoa(tb_PhieuTra.Text) == "")
            {
                MessageBox.Show("Bạn Nhập Thiếu Thông Tin Mã Phiếu Trả", "Thông Báo");
            }
            else
            {
                p1.SOPHIEUTRA = tb_PhieuTra.Text;
                p1.NGAYTRATHATSU = DateTime.Parse(dtp_NgayTra.Text);
                p1.MAPM = Fr_FirstPhieuTra.maPM;
                p.ThemPhieuTra(p1);
                dg_TraSach.DataSource = t.ThôngTinTraSach();
                
                int n = dg_TraSach.Rows.Count - 1;
                for (int i = 0; i < n; i++)
                {
                    dg_TraSach.Rows[i].Cells[0].Value = i + 1;
                    TimeSpan time = DateTime.Today - (DateTime)dg_TraSach.Rows[i].Cells[2].Value;
                    dg_TraSach.Rows[i].Cells[3].Value = time.TotalDays;
                    if (time.TotalDays - 4 > 0)
                        dg_TraSach.Rows[i].Cells[4].Value = (time.TotalDays - 4)*1000;
                }
                int tienPhat = 0;
                int tongNo = 0;
                for (int i = 0; i < n; i++)
                {
                    tienPhat += (int)dg_TraSach.Rows[i].Cells[4].Value; 
                }
                tb_TienPhat.Text = tienPhat.ToString();
                tongNo = int.Parse(tb_TienPhat.Text) + int.Parse(tb_TienNo.Text);
                tb_TongNo.Text = tongNo.ToString();
            }

        }
    }
}